/**
 * 给你两个字符串 word1 和 word2 。请你从 word1 开始，通过交替添加字母来合并字符串。
 * 如果一个字符串比另一个字符串长，就将多出来的字母追加到合并后字符串的末尾。
 */
var mergeAlternately = function(word1, word2) {
    let total = word1.length + word2.length
    let target = new Array(total), w1 = 0, w2 = 0

    for (let i = 0; i < total;i ++) {
        if (i % 2 == 0 && word1[w1]) {
            target[i] = word1[w1]
            w1 += 1
        } 
        if (i % 2 !== 0 && word2[w2]) {
            target[i] = word2[w2]
            w2 += 1
        }
        
        if (!word1[w1]) {
            target.push(word2.slice(w2))
            break
        }
        if (!word2[w2]) {
            target.push(word1.slice(w1))
            break
        }
    }
    return target.join('')
};

let word1 = "ab", word2 = "pqrs"
console.log(mergeAlternately(word1,word2))